@extends('template.index')
@section('main')
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>新增销售记录</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="/">首页</a></li>
                        <li class="breadcrumb-item active">新增销售记录</li>
                    </ol>
                </div>
            </div>
        </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="container-fluid">
            <div class="card card-primary">
                <!-- left column -->

                <!-- general form elements -->
                <div class="card-header">
                    <h3 class="card-title">基本信息</h3>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="form-group col-md-6">
                            <label>订单日期:</label>
                            <div class="input-group date">
                                <div class="input-group-prepend">
                                  <span class="input-group-text">
                                    <i class="fa fa-calendar"></i>
                                  </span>
                                </div>
                                <input type="text" class="form-control float-right" id="datepicker">
                            </div>
                            <!-- /.input group -->
                        </div>
                        <div class="form-group col-md-6">
                            <label>发货日期:</label>
                            <div class="input-group date">
                                <div class="input-group-prepend">
                                  <span class="input-group-text">
                                    <i class="fa fa-calendar"></i>
                                  </span>
                                </div>
                                <input type="text" class="form-control float-right" id="datepicker1">
                            </div>
                            <!-- /.input group -->
                        </div>
                        <div class="form-group col-md-6">
                            <label>收件人：</label>
                            <input type="text" class="form-control" id="receiver">
                        </div>
                        <div class="form-group col-md-6">
                            <label>收件人电话：</label>
                            <input type="text" class="form-control" id="mobilesj">
                        </div>
                        <div class="form-group col-md-6">
                            <label>收货省份：</label>
                            <select class="form-control" style="width: 100%;" id="province">
                                <option value="0">同城</option>
                                <option value="1">辽宁省</option>
                                <option value="2">黑龙江省</option>
                                <option value="3">吉林省</option>
                                <option value="4">河北省</option>
                                <option value="5">山东省</option>
                                <option value="6">北京市</option>
                                <option value="7">天津市</option>
                                <option value="8">河南省</option>
                                <option value="9">山西省</option>
                                <option value="10">上海市</option>
                                <option value="11">江苏省</option>
                                <option value="12">浙江省</option>
                                <option value="13">重庆市</option>
                                <option value="14">湖北省</option>
                                <option value="15">陕西省</option>
                                <option value="16">安徽省</option>
                                <option value="17">福建省</option>
                                <option value="18">江西省</option>
                                <option value="19">湖南省</option>
                                <option value="20">广东省</option>
                                <option value="21">海南省</option>
                                <option value="22">广西自治区</option>
                                <option value="23">贵州省</option>
                                <option value="24">云南省</option>
                            </select>
                        </div>
                        <div class="form-group col-md-6">
                            <label>收货地址：</label>
                            <input type="text" class="form-control" id="address">
                        </div>
                        <div class="form-group col-md-6">
                            <label>发货人：</label>
                            <input type="text" class="form-control" id="sender">
                        </div>
                        <div class="form-group col-md-6">
                            <label>发货人电话：</label>
                            <input type="text" class="form-control" id="sendermobile">
                        </div>
                        <div class="form-group col-12">
                            <label>备注：</label>
                            <textarea id="description" class="form-control" rows="2" placeholder="备注信息"></textarea>
                        </div>
                        <div class="form-group col-md-3">
                            <label>樱桃品种：</label>
                            <select class="form-control" style="width: 100%;" id="selectpz">
                                <option value="1">美早</option>
                                <option value="2">红灯</option>
                            </select>
                        </div>
                        <div class="form-group col-md-3">
                            <label>价格(元/斤)：</label>
                            <select class="form-control" style="width: 100%;" id="selectjg">
                                <option value="25">25</option>
                                <option value="28">28</option>
                            </select>
                        </div>
                        <div class="form-group col-md-3">
                            <label>樱桃重量(kg)：</label>
                            <div class="input-group-append">
                                <input type="text" id="weightcherry" onkeyup="value=isNumFloor(value)" placeholder="请输入数字" class="form-control float-left">
                                <span class="input-group-text">kg</span>
                            </div>
                        </div>
                        <div class="form-group col-md-3">
                            <label>发货重量(kg)：</label>
                            <div class="input-group-append">
                                <input type="text" id="weighttotal" onkeyup="value=isNumFloor(value)" class="form-control float-left" disabled>
                                <span class="input-group-text">kg</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card card-primary">
                <div class="card-header">
                    <h3 class="card-title">物流费用明细</h3>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="form-group col-md-4">
                            <label>运费:</label>
                            <input type="text" class="form-control" id="transportfee" disabled>
                        </div>
                        <div class="form-group col-md-4">
                            <label>包装费:</label>
                            <input type="text" class="form-control" value="6" id="baozhuang">
                        </div>
                        <div class="form-group col-md-4">
                            <label>合计金额:</label>
                            <input type="text" class="form-control text-danger" id="feetotal" disabled>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card card-primary">
                <div class="card-header">
                    <h3 class="card-title">快递信息</h3>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="form-group col-md-6">
                            <label>快递号:</label>
                            <input type="text" class="form-control" id="kuaidinumber">
                        </div>
                        <div class="form-group col-md-6">
                            <label>签收日期:</label>
                            <div class="input-group date">
                                <div class="input-group-prepend">
                                  <span class="input-group-text">
                                    <i class="fa fa-calendar"></i>
                                  </span>
                                </div>
                                <input type="text" class="form-control float-right" id="datepicker2">
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="card-footer text-center">
                <button type="submit" class="btn btn-danger col-5" id="delete">删除</button>
                <button type="submit" class="btn btn-primary col-5" id="submit">提交</button>
            </div>
        </div>
    </section>
@endsection
@section('footscript')
    <!-- DatePicker -->
    <script src="plugins/datepicker/bootstrap-datepicker.js"></script>
    <script src="plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"></script>
    <script>
        var id = getQueryString('id');
        id = id === null ? '' : id;
        var transportFee = [
            {name:'同城', oneKg: 14, addPerKg: 4},
            {name:'辽宁省', oneKg: 15, addPerKg: 4},
            {name:'黑龙江省', oneKg: 20, addPerKg: 10},
            {name:'吉林省', oneKg: 20, addPerKg: 10},
            {name:'河北省', oneKg: 24, addPerKg: 10},
            {name:'山东省', oneKg: 24, addPerKg: 10},
            {name:'北京市', oneKg: 24, addPerKg: 10},
            {name:'天津市', oneKg: 24, addPerKg: 10},
            {name:'河南省', oneKg: 25, addPerKg: 12},
            {name:'山西省', oneKg: 25, addPerKg: 12},
            {name:'上海市', oneKg: 25, addPerKg: 15},
            {name:'江苏省', oneKg: 25, addPerKg: 15},
            {name:'浙江省', oneKg: 25, addPerKg: 15},
            {name:'重庆市', oneKg: 25, addPerKg: 15},
            {name:'湖北省', oneKg: 25, addPerKg: 15},
            {name:'陕西省', oneKg: 25, addPerKg: 15},
            {name:'安徽省', oneKg: 25, addPerKg: 15},
            {name:'福建省', oneKg: 25, addPerKg: 15},
            {name:'江西省', oneKg: 25, addPerKg: 15},
            {name:'湖南省', oneKg: 25, addPerKg: 15},
            {name:'广东省', oneKg: 25, addPerKg: 16},
            {name:'海南省', oneKg: 25, addPerKg: 16},
            {name:'广西自治区', oneKg: 25, addPerKg: 16},
            {name:'贵州省', oneKg: 25, addPerKg: 16},
            {name:'云南省', oneKg: 25, addPerKg: 16}
        ];

        $(function () {
            getRecord();

            //Date picker
            $('#datepicker').datepicker({
                language: "zh-CN",
                autoclose: true,//选中之后自动隐藏日期选择框
                format: "yyyy-mm-dd"
            });

            //Date picker
            $('#datepicker1').datepicker({
                language: "zh-CN",
                autoclose: true,//选中之后自动隐藏日期选择框
                format: "yyyy-mm-dd"
            });

            //Date picker
            $('#datepicker2').datepicker({
                language: "zh-CN",
                autoclose: true,//选中之后自动隐藏日期选择框
                format: "yyyy-mm-dd"
            });
        });

        $('#province').change(function () {
            feeTotalFun();
        });

        $('#selectpz').change(function (e) {
            var domId = $('#selectpz');
            var domId1 = $('#selectjg');
            var value = domId.val();
            var options = '';
            switch (parseInt(value)){
                case 1://美早
                    options = '<option value="25">25</option><option value="28">28</option>';
                    break;
                case 2://红灯
                    options = '<option value="20">20</option>';
                    break;
            }
            domId1.empty();
            domId1.append(options);
            feeTotalFun();
        });

        $('#selectjg').change(function () {
            feeTotalFun();
        });

        $('#weighttotal').change(function () {
            feeTotalFun();
        });

        $('#baozhuang').change(function () {
            feeTotalFun();
        });

        $('#weightcherry').bind('input propertychange', function(){
            //设置发货重量显示
            var content = Number(isNumFloor(String($(this).val())));
            var domId = $('#weighttotal');
            content = content + 0.5;
            domId.attr('value', '');
            domId.attr('value', content > 0 ? content : '');
            feeTotalFun();
        });

        function feeTotalFun() {
            var province = parseInt($('#province').val());
            var weightTotal = Number($('#weighttotal').val());
            var baoZhuangFee = parseInt($('#baozhuang').val());//包装费
            var shuLiang = Number($('#weightcherry').val());
            var danJia = parseInt($('#selectjg').val()) * 2;
            if (province >= 0 && weightTotal >= 0 && baoZhuangFee >= 0 &&
                shuLiang >= 0 && danJia >= 0) {
                //计算运费
                var transportFeeData = transportFee[province];
                var oneKg = transportFeeData.oneKg;
                var addPerKg = transportFeeData.addPerKg;
                var transportFeee = (weightTotal - 1) * addPerKg + oneKg;
                $('#transportfee').attr('value', transportFeee);
                //计算总价
                var totalFee = transportFeee + baoZhuangFee + danJia * shuLiang;
                $('#feetotal').attr('value', totalFee);
            }
        }

        $('#submit').click(function () {
            public();
        });

        $('#delete').click(function () {
           deleteRecord();
        });
        function public() {
            commitTime = $('#datepicker').val();
            sendTime = $('#datepicker1').val();
            receiver = $('#receiver').val();
            receiverMobile = $('#mobilesj').val();
            province = $('#province').val();
            address = $('#address').val();
            sender = $('#sender').val();
            senderMobile = $('#sendermobile').val();
            description = $('#description').val();
            cherryClass = $('#selectpz').val();
            cherryPrice = $('#selectjg').val();
            cherryWeight = $('#weightcherry').val();
            faHuoWeight = $('#weighttotal').val();
            transportFeeInput = $('#transportfee').val();
            baozhuangFee = $('#baozhuang').val();
            total = $('#feetotal').val();
            kuaiDiNumber = $('#kuaidinumber').val();
            receiveTime = $('#datepicker2').val();
            var canSubmit = true;
            var tips = '';
            if (commitTime === ''){
                canSubmit = false;
                tips = '订单时间不能为空';
            }
            // if (sendTime === ''){
            //     canSubmit = false;
            //     tips = '发货时间不能为空';
            // }
            if (receiver === ''){
                canSubmit = false;
                tips = '收货人不能为空';
            }
            if (isMobile(receiverMobile) === false){
                canSubmit = false;
                tips = '收货人手机号码不正确';
            }
            if (province === ''){
                canSubmit = false;
                tips = '省份不能为空';
            }
            if (address === ''){
                canSubmit = false;
                tips = '地址不能为空';
            }
            if (sender === ''){
                canSubmit = false;
                tips = '发件人不能为空';
            }
            if (isMobile(senderMobile) === false){
                canSubmit = false;
                tips = '发件人手机号码不正确';
            }
            if (cherryClass === ''){
                canSubmit = false;
                tips = '樱桃品种不能为空';
            }
            if (cherryPrice === ''){
                canSubmit = false;
                tips = '樱桃单价不能为空';
            }
            if (cherryWeight === ''){
                canSubmit = false;
                tips = '樱桃重量不能为空';
            }
            if (faHuoWeight === ''){
                canSubmit = false;
                tips = '发货重量不能为空';
            }
            if (transportFeeInput === ''){
                canSubmit = false;
                tips = '运费不能为空';
            }
            if (baozhuangFee === ''){
                canSubmit = false;
                tips = '包装费不能为空';
            }
            if (total === ''){
                canSubmit = false;
                tips = '总价不能为空';
            }
            if (canSubmit === true) {
                $.post(
                    'add',
                    {
                        commitTime: commitTime,
                        sendTime: sendTime,
                        receiver: receiver,
                        receiverMobile: receiverMobile,
                        province: province,
                        address: address,
                        sender: sender,
                        senderMobile: senderMobile,
                        description: description,
                        cherryClass: cherryClass,
                        cherryPrice: cherryPrice,
                        cherryWeight: cherryWeight,
                        faHuoWeight: faHuoWeight,
                        transportFee: transportFeeInput,
                        baozhuangFee: baozhuangFee,
                        total: total,
                        kuaiDiNumber: kuaiDiNumber,
                        receiveTime: receiveTime,
                        id : id
                    },
                    function (result) {
                        result = JSON.parse(result);
                        alert(result.content);
                        if (result.status === 0){
                            $(window).attr('location', 'table');
                        }
                    }
                );
            }
            else {
                alert(tips);
            }
        }

        function isNumFloor(value) {
            value = value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
            value = value.replace(/^\./g,""); //验证第一个字符是数字
            value = value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
            value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
            value = value.replace(/^(\-)*(\d+)\.(\d).*$/,'$1$2.$3'); //只能输入两个小数
            return value;
        }

        function isMobile(mobile) {
            var myPreg = /^1[34578]\d{9}$/;
            if (myPreg.test(mobile)){
                return true;
            }
            else {
                return false;
            }
        }

        function getRecord() {
            if (getQueryString('id') !== null) {
                $.get(
                    'addJson',
                    {
                        id : id
                    },
                    function (result) {
                        var data = JSON.parse(result).data;
                        $('#datepicker').attr('value', data.commit_time);
                        $('#datepicker1').attr('value', data.send_time);
                        $('#receiver').attr('value', data.receiver);
                        $('#mobilesj').attr('value', data.receiver_mobile);
                        $('#address').attr('value', data.address);
                        $('#sender').attr('value', data.sender);
                        $('#sendermobile').attr('value', data.sender_mobile);
                        $('#weightcherry').attr('value', data.cherry_weight);
                        $('#weighttotal').attr('value', data.cherry_fahuo_weight);
                        $('#description').attr('value', data.description);
                        $('#transportfee').attr('value', data.transport_fee);
                        $('#baozhuang').attr('value', data.baozhuang_fee);
                        $('#feetotal').attr('value', data.total);
                        $('#kuaidinumber').attr('value', data.kuaidi_number);
                        $('#datepicker2').attr('value', data.receive_time);
                        selectPickerSelected($('#province'), data.province);
                        selectPickerSelected($('#selectpz'), data.cherry_class);
                        var domId1 = $('#selectjg');
                        var value = data.cherry_class;
                        var options = '';
                        switch (parseInt(value)){
                            case 1://美早
                                options = '<option value="25">25</option><option value="28">28</option>';
                                break;
                            case 2://红灯
                                options = '<option value="20">20</option>';
                                break;
                        }
                        domId1.empty();
                        domId1.append(options);
                        selectPickerSelected($('#selectjg'), data.cherry_price);
                    }
                );
            }
            else {
                $('#delete').hide();
            }
        }

        function deleteRecord() {
            $.post(
                'delete',
                {
                    id : id
                },
                function (result) {
                    var data = JSON.parse(result);
                    if (data.status === 0){
                        $(window).attr('location', '/');
                    }
                    else {
                        alert(data.content);
                    }
                }
            );
        }
    </script>
@endsection